package com.xm.xmds1705083.service.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xm.xm.dto.ResultDto;
import com.xm.xm.dto.SearchDto;
import com.xm.xm.util.ErrorCode;
import com.xm.xm.util.ValidateUtils;
import com.xm.xmds1705083.service.dto.DuoduoPassLogDto;
import com.xm.xmds1705083.service.mapper.DuoduoParkMapper;
import com.xm.xmds1705083.service.mapper.DuoduoPassLogMapper;
import com.xm.xmds1705083.service.mapper.DuoduoUserCarMapper;
import com.xm.xmds1705083.service.model.DuoduoPark;
import com.xm.xmds1705083.service.model.DuoduoPassLog;
import com.xm.xmds1705083.service.model.DuoduoUserCar;
import com.xm.xmds1705083.service.model.SocketEntity;
import com.xm.xmds1705083.service.service.DuoduoPassLogService;
import com.xm.xmds1705083.service.util.GlobalError;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 杭州新苗网络科技有限公司
 * @author meihf
 * @date 2017/06/15
 * @description
*/ 
@Service("duoduoPassLogService")
public class    DuoduoPassLogServiceImpl extends ServiceImpl<DuoduoPassLogMapper,DuoduoPassLog> implements DuoduoPassLogService {

    @Autowired
    DuoduoPassLogMapper duoduoPassLogMapper;

    @Autowired
    DuoduoUserCarMapper duoduoUserCarMapper;

    @Autowired
    DuoduoParkMapper duoduoParkMapper;

    static Logger logger = LoggerFactory.getLogger(DuoduoPassLogServiceImpl.class);

    @Override
    public ResultDto findMyPassLog(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        searchDto.setPageNo((searchDto.getPageNo()-1)*searchDto.getPageSize());
        try {
             //先查我的车牌号 再到记录表里去匹配
            Map<String,Object> paramMap = new HashedMap();
            paramMap.put("userId",searchDto.getAppUserId());
            paramMap.put("isDelete",0);
            List<DuoduoUserCar> duoduoUserCarList = duoduoUserCarMapper.selectByMap(paramMap);
            if (duoduoUserCarList.size()==0){
                resultDto.setCode(ErrorCode.C11001);
                resultDto.setMsg(ErrorCode.E11001);
                return resultDto;
            }
            String mediaNo = duoduoUserCarList.get(0).getMediaNo();
            JSONObject p = new JSONObject();
            p.put("mediaNo",mediaNo);
            p.put("pageNo",searchDto.getPageNo());
            p.put("pageSize",searchDto.getPageSize());
            List<DuoduoPassLogDto> duoduoPassLogDtoList = duoduoPassLogMapper.findMyPassLogs(p);
            if (duoduoPassLogDtoList.size()==0){
                resultDto.setCode(ErrorCode.C11001);
                resultDto.setMsg(ErrorCode.E11001);
                return resultDto;
            }else {
                resultDto.setData(duoduoPassLogDtoList);
                resultDto.setMsg("查询成功");
            }
        }catch (RuntimeException e ){
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }




    @Override
    public ResultDto parkInfoSyn(SocketEntity entity) {
        ResultDto resultDto = new ResultDto();
        JSONObject filed = new JSONObject();
        try {
            JSONObject params = JSONObject.fromObject(entity.getParam());
            if (!filed.has("recordID") || !filed.has("datasrc")
                    ||!filed.has("inputType") || !filed.has("mediaNo")
                    ||!filed.has("inputTime") || !filed.has("enterStatus")
                    ||!filed.has("areaId")){
                logger.info(ErrorCode.E10011);
            }else{
                String recordId = params.getString("recordID");
                String datasrc = params.getString("datasrc");
                String inputType = params.getString("inputType");
                String mediaNo = params.getString("mediaNo");
                String inputTime = params.getString("inputTime");
                String enterStatus = params.getString("enterStatus");
                String areaId = params.getString("areaId");
                //将信息存进库中
                DuoduoPassLog duoduoPassLog = new DuoduoPassLog();
                duoduoPassLog.setInputTime(inputTime);
                duoduoPassLog.setMediaNo(mediaNo);
                duoduoPassLog.setType(1);
                duoduoPassLog.setParkId(Integer.parseInt(entity.getTermid()));
                int result = duoduoPassLogMapper.insert(duoduoPassLog);
                if (ValidateUtils.isGreaterThan0(result)){
            //通过车的牌号和type=1类型获取停车场的parkId 从而获取停车场的信息，在剩余停车位上减一；
                    EntityWrapper entityWrapper = new EntityWrapper();
                    entityWrapper.where(" mediaNo={0} and type=1",mediaNo);
                    List<DuoduoPassLog> duoduoPassLogs = duoduoPassLogMapper.selectList(entityWrapper);
                    if (duoduoPassLogs.size()==0){
                        resultDto.setCode(ErrorCode.C11001);
                        resultDto.setMsg(ErrorCode.E11001);
                    }else{
                       int parkId= duoduoPassLogs.get(0).getParkId();
                        EntityWrapper entityWrapper1 = new EntityWrapper();
                        entityWrapper1.where(" id={0}",parkId);
                        List<DuoduoPark>listparks = duoduoParkMapper.selectList(entityWrapper1);
                        if (listparks.size()==0) {
                            resultDto.setCode(ErrorCode.C11001);
                            resultDto.setMsg(ErrorCode.E11001);
                        }else{
                           int surplusSpace= listparks.get(0).getSurplusSpace()-1;
                            DuoduoPark p=new DuoduoPark();
                            p.setSurplusSpace(surplusSpace);
                            int updateResult= duoduoParkMapper.update(p,entityWrapper1);
                            if (ValidateUtils.isGreaterThan0(updateResult)){

                                filed.put("recordID",recordId);
                                filed.put("result","00");
                                filed.put("payType","00");
                                filed.put("rsv1","");
                                filed.put("rsv2","");
                                resultDto.setMsg("同步成功");

                            }else {
                                resultDto.setMsg(ErrorCode.E11003);
                                resultDto.setCode(ErrorCode.C11003);
                            }

                        }
                    }


                }else {
                    filed.put("recordID",recordId);
                    filed.put("result","01");
                    filed.put("payType","00");
                    filed.put("rsv1","");
                    filed.put("rsv2","");
                    resultDto.setMsg("同步失败");
                    logger.info("同步失败");
                }
            }
            resultDto.setData(filed);

        }catch (RuntimeException e ){
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

    @Override
    public ResultDto carOutInfo(SocketEntity entity) {
        ResultDto resultDto = new ResultDto();
        JSONObject filed = new JSONObject();
        try {
            if (!filed.has("recordID") || !filed.has("datasrc")
                    || !filed.has("mediaNo") || !filed.has("inputTime")
                    || !filed.has("outputTime") ||!filed.has("parkCount")){
                logger.info(ErrorCode.E10011);
            }else{
                JSONObject params = JSONObject.fromObject(entity.getParam());
                String recordId = params.getString("recordID");
                String datasrc = params.getString("datasrc");
                String mediaNo = params.getString("mediaNo");
                String inputTime = params.getString("inputTime");
                String outputTime = params.getString("outputTime");
                int parkCount = params.getInt("parkCount");
                EntityWrapper entityWrapper = new EntityWrapper();
                entityWrapper.where(" inputTime={0} and mediaNo={1} and type = 2",inputTime,mediaNo);
                List<DuoduoPassLog> passLogs =  duoduoPassLogMapper.selectList(entityWrapper);
                if (passLogs.size()==0){
                    resultDto.setCode(GlobalError.C11029);
                    resultDto.setMsg(GlobalError.E11029);
                    return resultDto;
                }
                DuoduoPassLog duoduoPassLog = passLogs.get(0);
                duoduoPassLog.setType(2);
                duoduoPassLog.setOutputTime(outputTime);
                duoduoPassLog.setParkCount(parkCount);
                int result = duoduoPassLogMapper.updateById(duoduoPassLog);
                if (ValidateUtils.isGreaterThan0(result)){

                    //通过车的牌号和type=2类型获取停车场的parkId 从而获取停车场的信息，在剩余停车位上加一；
                    EntityWrapper entityWrapper2 = new EntityWrapper();
                    entityWrapper2.where(" mediaNo={0} and type=2",mediaNo);
                    List<DuoduoPassLog> duoduoPassLogs = duoduoPassLogMapper.selectList(entityWrapper2);
                    if (duoduoPassLogs.size()==0){
                        resultDto.setCode(ErrorCode.C11001);
                        resultDto.setMsg(ErrorCode.E11001);
                    }else{
                        int parkId= duoduoPassLogs.get(0).getParkId();
                        EntityWrapper entityWrapper1 = new EntityWrapper();
                        entityWrapper1.where(" id={0}",parkId);
                        List<DuoduoPark>listparks = duoduoParkMapper.selectList(entityWrapper1);
                        if (listparks.size()==0) {
                            resultDto.setCode(ErrorCode.C11001);
                            resultDto.setMsg(ErrorCode.E11001);
                        }else{
                            int surplusSpace= listparks.get(0).getSurplusSpace()+1;
                            DuoduoPark p=new DuoduoPark();
                            p.setSurplusSpace(surplusSpace);
                            int updateResult= duoduoParkMapper.update(p,entityWrapper1);
                            if (ValidateUtils.isGreaterThan0(updateResult)){
                                filed.put("recordID",recordId);
                                filed.put("result","00");
                                filed.put("rsv1","");
                                filed.put("rsv2","");
                                resultDto.setMsg("同步成功");
                                logger.info("同步成功");
                            }else {
                                resultDto.setMsg(ErrorCode.E11003);
                                resultDto.setCode(ErrorCode.C11003);
                            }

                        }
                    }
                }else {
                    filed.put("recordID",recordId);
                    filed.put("result","01");
                    filed.put("rsv1","");
                    filed.put("rsv2","");
                    resultDto.setMsg("同步失败");
                    logger.info("同步失败");
                }
            }
            resultDto.setData(filed);
        }catch (RuntimeException e ){
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

}

